package com.sjq.yqgl.web;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sjq.common.entity.LoginAccount;
import com.sjq.common.util.ExportExcelUtil;
import com.sjq.common.util.FTPClientHelper;
import com.sjq.common.util.ImportsExcel;
import com.sjq.common.util.PageResults;
import com.sjq.common.util.PrimaryKeyUtil;
import com.sjq.common.web.SessionUserHelper;
import com.sjq.yqgl.entity.Yqbs;
import com.sjq.yqgl.entity.Yqfj;
import com.sjq.yqgl.entity.Yqxf;
import com.sjq.yqgl.service.YqfjService;
import com.sjq.yqgl.service.YqxfService;

@Controller
public class YqfjController {
	@Autowired
    private YqfjService yqfjService;
	@Autowired
    private YqxfService yqxfService;
    @Autowired
    private HttpSession session;  
    private static final String INITURL = "yqfj/"; 
   /**查询列表***/
   @RequestMapping(INITURL+"YqfjList")
   @ResponseBody
   public JSONObject  YqfjList(Integer pageNo, Integer pageSize, Yqfj yqfj){
 	  if(yqfj==null){
    		yqfj=new Yqfj();
    	}
   	   pageNo = pageNo == null ? 1 : pageNo;
   	  SessionUserHelper helper = SessionUserHelper.createBySession(session);
      LoginAccount la= helper.getLoginAccount();
      String role=la.getUserRole();
     String fjlx= yqfj.getFjlx();
     if(fjlx.equals("0")){
    	 
    	 
     }else {
    	 if(role.equals("0001")){
     	  
    	 }else  if(role.equals("0002")){
     		   //分县局上报
     		   yqfj.setDjrjh(la.getAccountId());
     	   }
     }
   	  
       PageResults<?> pageResults=yqfjService.findYqfjByPage(pageNo, PageResults.ALLPAGESIZE, yqfj);
       JSONObject json=new JSONObject();
       json.put("dataList", pageResults.getResults());
       json.put("pageNo",  pageResults.getPageNo());
       json.put("pages", pageResults.getPageCount());
       return json;
   }
   
    /**提交表单---不含文件**/
    @RequestMapping(INITURL+"addYqfj")
	@ResponseBody
	 public JSONObject addYqfj(Yqfj yqfj) {
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       return yqfjService.addYqfj(yqfj, la);
    }
    
    /**提交表单---含附件**/
    @RequestMapping(INITURL+"addYqfjFiles")
    @ResponseBody
	 public JSONObject addYqfjFiles(Yqfj yqfj,@RequestParam("fj") CommonsMultipartFile[]  fj) {
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       return  yqfjService.addYqfjFilesByFTP(yqfj,fj, la);
    }
    @RequestMapping(INITURL+"showFjInfo")
    @ResponseBody
	 public List<List<Object>> showFjInfo(Yqfj yqfj,@RequestParam("fj") CommonsMultipartFile[]  fj) {
    	List<List<Object>> nrs=new ArrayList<List<Object>>();
       try {
    	   nrs= ImportsExcel.readExcel(fj[0]);
       } catch (IOException e) {
    	   e.printStackTrace();
       }
       
       return  nrs;
    }
    
    /**下载附件***/
    @RequestMapping(INITURL+"downFiles")
 	public ResponseEntity<byte[]>  downFiles(String id) throws IOException{
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       HttpHeaders headers=new HttpHeaders();
       headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
       Yqfj yqfj=new Yqfj ();
       yqfj.setXxzjbh(id);
       PageResults<?> pageResults=yqfjService.findYqfjByPage(1, PageResults.ALLPAGESIZE, yqfj);
       String filename="";
       if(pageResults.getResults().size()>0){
    	   Map <String,String>data=(Map <String,String>)	pageResults.getResults().get(0);
            filename=data.get("fjmc");
           headers.setContentDispositionFormData("attachment", filename);
       }else {
    	   
    	   
    	   
    	   
       }   
       //读取FTP 上面的 附件
       FTPClientHelper ftp=new FTPClientHelper("\\"+la.getUserId()+"\\");
       ResponseEntity<byte[]> nrs=new ResponseEntity<byte[]>(ftp.downFileByte("",filename), headers, org.springframework.http.HttpStatus.CREATED);
       return  nrs;
    }
 	
 	
 	/**生成上报表格的条件的附件***/
 	@RequestMapping(INITURL+"downSbExcel")
 	public void  downSbExcel(String id,HttpServletResponse resp) throws IOException{
	   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       LoginAccount la= helper.getLoginAccount();
       Yqxf yqfj=new Yqxf ();
       yqfj.setGlrwid(id);
 
      String role=la.getUserRole();
   	   if(role.equals("0001")){
   	   }else  if(role.equals("0002")){
   		   //分县局上报
   		   yqfj.setXfj(la.getUserDwmc());
   	   }
       PageResults<?> pageResults=yqxfService.findYqxfByPage(1,10000, yqfj);//单次最大导入10000条
       String filename="";
       OutputStream out=  resp.getOutputStream();
       resp.setContentType("application/vnd.ms-excel");
       String contentDisposition = "";
       contentDisposition = "attachment; filename=" + PrimaryKeyUtil.getUUID()+".xls";
       
       resp.setHeader("Content-Disposition", contentDisposition);
       resp.setCharacterEncoding("UTF-8");
       if(pageResults.getResults().size()>0){
    		ExportExcelUtil<Yqbs> ex=new ExportExcelUtil();
        	ArrayList<Yqbs> dataset=new ArrayList<Yqbs>(); 
        	 String[] headers1={"序号","姓名","手机号码","身份证号","户籍地址","现住地址","核查方式（电话/见面）","回宿时间(格式为：20200110)","当前位置（是否在宿 ，填  是、否）","有无身体不适（发热/咳嗽/其它呼吸道症状/无）","防护措施（居家观察/医学隔离）","社区包保人员","联系电话","社区民警","联系电话"};
        	 List<Map<String,String>> nrs=(List<Map<String,String>> )	pageResults.getResults();
        	 for(int i=0;i<nrs.size();i++){
        		 Map <String,String> data=(Map <String,String>)nrs.get(i);
        		 String xm=data.get("xm");
        		 String sfhm=data.get("sfhm");
        		 String sjhm=data.get("sjhm");
        		 String dz=data.get("dz");
        		 Yqbs exp=new Yqbs();
        		 exp.setXm(xm);
            	 exp.setSjhm(sjhm);
            	 exp.setSfhm(sfhm); 
        		 exp.setHjdz(dz);
        		 dataset.add(exp);
        	 }
        	 String title="下发数据核查表（ xx  月   xx 日 上报）";
        	 ex.exportExcel("市局下发疫情核查名单",title, headers1, dataset, out);
       }else {
    
       
       }   
       //生成附件
    }
    
    
    /**删除信息**/
     @RequestMapping(INITURL+"deleteYqfj")
	 @ResponseBody
	 public JSONObject deleteYqfj(String yqfjId,String fjlx) {
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount la= helper.getLoginAccount();
        String role=la.getUserRole();
        JSONObject res=new JSONObject();
        if(fjlx.equals("0")){
        	//下发
        	 if(role.equals("0001")){
             	return yqfjService.deleteYqfj(yqfjId, fjlx, la);
         	 }else {
         		 res.put("code", "300");
         		 res.put("msg", "下发分县局，无权进行操作");
         		 return  res;
         	 }
        }else if(fjlx.equals("1")){
        	//上报
        	return yqfjService.deleteYqfj(yqfjId, fjlx, la);
        }else if(fjlx.equals("2")){
        	//密切关注者
        	return yqfjService.deleteYqfj(yqfjId, fjlx, la);
        }
        return null;
     }	
    
    
    /**更新表单**/
     @RequestMapping(INITURL+"motifyYqfj")
	 @ResponseBody
	 public JSONObject motifyYqfj(Yqfj yqfj) {
        SessionUserHelper helper = SessionUserHelper.createBySession(session);
        LoginAccount la= helper.getLoginAccount();
        return  yqfjService.updateYqfj(yqfj,la);   
    }	
    
    
     /**
    * @description 根据查询条件查询表的所有详细信息
    **/
	@RequestMapping(INITURL+"findYqfjAllList")
    @ResponseBody
    public JSONObject  findYqfjAllList(Yqfj yqfj){
 		   SessionUserHelper helper = SessionUserHelper.createBySession(session);
       	   LoginAccount la= helper.getLoginAccount();
		   return  yqfjService.findYqfjByListAll(yqfj,la);  	
     }
   
   
}
